import {Injectable} from '@angular/core';
import {env} from "@env/environment";

/*
  Generated class for the EnvironmentProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class EnvironmentProvider {

  env = env;

  constructor() {
    this.initInterface();
  }

  setServiceUrl(item) {
    let ssl = item['ssl'];
    let url = item['address'];
    let wsUrl = item['ws'];

    let getUrl = (i, isWebSocket?: boolean) => {
      let urlHead = "";
      if (isWebSocket) {
        urlHead = "ws"
      } else {
        urlHead = "http"
      }
      if (ssl) {
        urlHead += "s://"
      } else {
        urlHead += "://"
      }
      return urlHead += i;
    };

    let app_serve_url = getUrl(url);
    let ws_serve_url = getUrl(wsUrl, true);

    localStorage.setItem("app_serve_url", app_serve_url);
    localStorage.setItem("ws_serve_url", ws_serve_url);

    this.initInterface();
  }

  initInterface() {
    env.app_serve_url = localStorage.getItem("app_serve_url") || env.app_serve_url;
    env.ws_serve_url = localStorage.getItem("ws_serve_url") || env.ws_serve_url;
  }
}
